www.gusucode.com > 超声波测量以及形成图像 对相关信号进行模拟仿真 > 超声波测量以及形成图像 对相关信号进行模拟仿真/digital holograpy/prog/paste.m

    function [ varargout ] = paste( a,b,varargin )
%PASTE Paste an array to another
%  Syntax:
%  c = paste(a,b,m,n)
%  c = paste(a,b,n)
%  c = paste(a,b)
%  paste(a,b,...);
%
%  paste b to a
%  a and b must be one or two-dimensional array
%  size of a and b can be different
%  m,n decides the position where b is pasted on a
%  if m,n are not inputted, it is the centre(origin of coordinates) of a
%
%  if there is no output, the result figure will be displayed
%
error(nargchk(2,4,nargin))
if nargout>1
    error('Too many output arguments')
end
[Ma,Na]=size(a);
[Mb,Nb]=size(b);
%------------------------------
if rem(Na,2)==0
    Oan=Na/2+1;
else
    Oan=(Na+1)/2;
end
if rem(Ma,2)==0
    Oam=Ma/2+1;
else
    Oam=(Ma+1)/2;
end
%------------------------------
if rem(Nb,2)==0
    Obn=Nb/2+1;
else
    Obn=(Nb+1)/2;
end
if rem(Mb,2)==0
    Obm=Mb/2+1;
else
    Obm=(Mb+1)/2;
end
%------------------------------
switch nargin
    case 2
        m=Oam;
        n=Oan;
    case 3
        if Na==1
            m=varargin{1};
            n=1;
        else
            m=Oam;
            n=varargin{1};
        end
    case 4
        m=varargin{1};
        n=varargin{2};
end
%------------------------------
anumleft=n-1;
anumright=Na-n;
anumup=m-1;
anumbelow=Ma-m;
bnumleft=Obn-1;
bnumright=Nb-Obn;
bnumup=Obm-1;
bnumbelow=Mb-Obm;
%------------------------------
numleft=min(anumleft,bnumleft);
numright=min(anumright,bnumright);
numup=min(anumup,bnumup);
numbelow=min(anumbelow,bnumbelow);
%------------------------------
c=a;
c([m-numup:m+numbelow],[n-numleft:n+numright])=b([Obm-numup:Obm+numbelow],[Obn-numleft:Obn+numright]);
switch nargout
    case 0
        if Ma==1||Na==1
            plot(c);
        else
            imagesc(c);colormap(gray);axis image;
        end
    case 1
        varargout{1}=c;
end